package com.qdxwx.manage.controller.role;

import com.qdxwx.common.exception.BusinessException;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.RoleService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.manager.Role;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 角色信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/role")
public class RoleEditController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     */
    @GetMapping("/add.html")
    public String add() {
        return "role/role-add";
    }

    /**
     * 新增保存角色
     */
    @Permissions("role:edit")
    @Log(title = "添加角色", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult add(HttpServletRequest request) {
        String roleName = request.getParameter("roleName");
        if (TKString.isEmpty(roleName)) return error("角色名称不能为空");
        if (roleService.checkRoleName(roleName)) return error("角色名称已存在");
        String roleKey = request.getParameter("roleKey");
        if (TKString.isEmpty(roleKey)) return error("角色权限字符不能为空");
        if (roleService.checkRoleKey(roleKey)) return error("角色权限已存在");
        Integer[] menuIds = TKConvert.toIntArray(request.getParameter("menuIds"));
        if (menuIds.length < 1) return error("至少选择一项权限");
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleKey(roleKey);
        role.setMenuIds(menuIds);
        role.setSort(TKConvert.toInteger(request.getParameter("sort")));
        role.setStatus("0");
        role.setRemark(request.getParameter("remark"));
        role.setCreateBy(SessionUtils.getLoginName());
        int row = roleService.insert(role);
        return toAjax(row);
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int roleId = TKConvert.toInteger(request.getParameter("id"));
        if (roleId < 1) return error404;
        Role role = roleService.get(roleId);
        if (role == null) return error404;
        mmap.put("role", role);
        return "role/role-edit";
    }

    /**
     * 修改保存角色
     */
    @Permissions("role:edit")
    @Log(title = "编辑角色", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult edit(HttpServletRequest request) {
        int roleId = TKConvert.toInteger(request.getParameter("roleId"));
        if (roleId < 1) return error("缺少id参数");
        Role role = roleService.get(roleId);
        Manager manager = SessionUtils.getManager();
        //if (!manager.isAdmin()) {
        //    if (role.isAdmin()) return error("您无权操作超级管理员角色");
        //}
        Integer[] menuIds = TKConvert.toIntArray(request.getParameter("menuIds"));
        if (menuIds.length < 1) return error("至少选择一项权限");
        role.setMenuIds(menuIds);
        role.setSort(TKConvert.toInteger(request.getParameter("sort")));
        role.setRemark(request.getParameter("remark"));
        role.setUpdateBy(manager.getLoginName());
        int row = roleService.update(role);
        return toAjax(row);
    }

    @Permissions("role:remove")
    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (TKString.isEmpty(ids)) return error("缺少id参数");
        try {
            return toAjax(roleService.deleteByIds(ids));
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 角色状态修改
     */
    @Log(title = "角色状态", businessType = BusinessType.UPDATE)
    @Permissions("role:edit")
    @PostMapping("/status.json")
    @ResponseBody
    public AjaxResult status(HttpServletRequest request) {
        int roleId = TKConvert.toInteger(request.getParameter("roleId"));
        String status = request.getParameter("status");
        if (roleId < 1 || TKString.isEmpty(status)) return error("参数异常");
        Role role = new Role();
        role.setRoleId(roleId);
        role.setStatus(status);
        try {
            roleService.checkRoleAllowed(role);
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
        return toAjax(roleService.updateStatus(role));
    }
}